package com.vercal.demoverticalviewpager.view;

import android.util.Log;
import android.view.View;


import androidx.viewpager.widget.ViewPager;

public class AoSmithTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;

    private String TAG = "AoSmithTransformer";

    @Override
    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();
        float alpha = 0;
        Log.e(TAG,"position="+position);
        if (0 <= position && position <= 1) {
            alpha = 1 - position;
            Log.e(TAG,"[0,1]");
        } else if (-1 < position && position < 0) {
            Log.e(TAG,"-1<x<0");
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float verticalMargin = pageHeight * (1 - scaleFactor) / 2;
            float horizontalMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                view.setTranslationX(horizontalMargin - verticalMargin / 2);
            } else {
                view.setTranslationX(-horizontalMargin + verticalMargin / 2);
            }
            view.setTranslationX(0);
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);
            alpha = position + 1;
        }

        view.setAlpha(alpha);
        view.setTranslationX(pageWidth * -position);
        float yPosition = position * pageHeight;
        view.setTranslationY(yPosition);

    }
}
